<?php
namespace Home\Controller;
use Think\Controller;
class ShowController extends CommonController {
    public function index(){
		/*获得父级分类*/
		$cid=I('get.cid',null,'intval');
		$cate=M('category')->select();
		$parentCate=$this->parent_cate($cate, $cid);
		
		/*面包屑*/
		$this->assign('mbx',array_reverse($parentCate));
		
		/*调用提问用户信息*/
		$this->getInfo();
		
		/*全部回答*/
		$asid=I('get.asid',null,'intval');
		$answer=D('answer');
		$count=$answer->where(array('asid'=>$asid))->count();
		$page=new \Think\Page($count,4);
		$answerList=$answer->where(array('asid'=>$asid))->order('time desc')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('page',$page->show());
		$this->assign('answerList',$answerList);
		$this->assign('face',$this->face($answerList));//分配头像
		$this->assign('ancount',$count);
		
		
		/*满意回答*/
		$adoptAnswer=$answer->where(array('adopt'=>1,'asid'=>$asid))->find();
		$this->assign('adoptAnswer',$adoptAnswer);
		$this->assign('ulevel',$this->exp_to_level($adoptAnswer['exp']));
		//$this->assign('uface',$this->face($adoptAnswer));//分配头像
		
		
		/*待解决相关问题*/
		$solveWhere['cid']=$cid;
		$solveWhere['solve']=0;
		$solveWhere['asid']=array('NEQ',$asid);
		$solving=M('ask')->field('content,time,answer,asid,cid')->where($solveWhere)->limit(5)->select();
		$this->assign('solving',$solving);
		
		
		
        $this->display('show');
    }
	
	//视图模型  获取问答用户信息
	public function getInfo(){
		$asid=I('get.asid',null,'intval');
		$askDate=D('ask');
		$askInfo=$askDate->where(array('asid'=>$asid))->find();
		$this->assign('adoptBfb',$this->adoptBfb($askInfo['answer'], $askInfo['adopt']));
		$this->assign('level',$this->exp_to_level($askInfo['exp']));
		$this->assign('askInfo',$askInfo);
	}
	
	/*回答问题*/
	public function answer(){
		if(!IS_POST) $this->error('页面不存在！');
		//组合插入数据
		$uid=I('session.uid',null,'intval');
		$data=array(
			'asid'=>I('post.asid',null,'intval'),
			'content'=>I('post.content'),
			'time'=>time(),
			'uid'=>$uid,
		);
		
		M('answer')->add($data);
		$user=M('user');
		$ask=M('ask');
		$ask->where(array('asid'=>$data['asid']))->setInc('answer',1);
		$user->where(array('uid'=>$uid))->setInc('answer',1);
		$user->where(array('uid'=>$uid))->setInc('exp',C('ANSWER_EXP'));
		$user->where(array('uid'=>$uid))->setInc('gold',C('ANSWER_GOLD'));
		$this->success('回复成功！');
	}
	
	/*采纳*/
	public function adopt(){
		$user=M('user');
		$answer=M('answer');
		$ask=M('ask');
		
		$anid=I('get.anid',null,'intval');								
		$uid=$answer->where(array('anid'=>$anid))->getField('uid');			      //获取回答用户的id
		
		$asid=I('get.asid',null,'intval');										  //获取问题的asid					
		$ask->where(array('asid'=>$asid))->save(array('solve'=>1));               //问题更新为已经解决
		$gold=$ask->where(array('asid'=>$asid))->getField('gold');                //获取问题的悬赏金币
		$user->where(array('uid'=>$uid))->setInc('gold',$gold);                   //采纳用户金币+悬赏金币
		
		$answer->where(array('anid'=>$anid))->save(array('adopt'=>1));			  //更新为采纳答案
		$user->where(array('uid'=>$uid))->setInc('adopt',1); 					  //回答用户采纳数+1
		$user->where(array('uid'=>$uid))->setInc('exp',C('ADOPT_EXP')); 		  //回答用户经验+
		$user->where(array('uid'=>$uid))->setInc('gold',C('ADOPT_GOLD')); 		  //回答用户金币+
		
		$this->success('采纳成功！');
	}
	
	
}